﻿#include "PhysicsDialogUniform.h"
#include "ui_PhysicsDialogUniform.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyUniform.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include <QMessageBox>
namespace GUI
{
    PhysicsDialogUniform::PhysicsDialogUniform(PhysicsDlgExecType type, int propid/*=-1*/, QWidget* parent /*= nullptr*/)
        :AbstractMaterialDialog(type, parent)
    {
        _ui = new Ui::PhysicsDialogUniform;
        _ui->setupUi(this);
        //初始化材料下拉列表
        if (!_matMgr)return;
        QList<Acoustics::FITKAcousticsAbsMaterial*> list = _matMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::ISOSolid);
        if (!list.isEmpty()) {
            for (auto mate : list) {
                _ui->comboBox_material->addItem(mate->getDataObjectName(), mate->getDataObjectID());
            }
        }
        //获取材料对象
        if (_propMgr && propid > 0)
            _prop = dynamic_cast<Acoustics::FITKPhyscisPropertyUniform*>(_propMgr->getDataByID(propid));

        this->init();

    }
    PhysicsDialogUniform::~PhysicsDialogUniform()
    {
        if (_ui) delete _ui;
    }
    void PhysicsDialogUniform::on_pushButtonOK_clicked()
    {
        if (!this->checkInput()) return;
        //要处理的对象
        Acoustics::FITKPhyscisPropertyUniform* uniform = dynamic_cast<Acoustics::FITKPhyscisPropertyUniform*>(_prop);
        //保存执行的脚本
        QStringList str;
        QString name = _ui->lineEdit_name->text();

        //创建与复制,创建新对象
        if (_execType == GUI::PhysicsDlgExecType::Create || _execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            str.append("p=PhyscisProp.Uniform()");
            str.append(QString("p.createPhyscisProp('%1')").arg(name));

            uniform = new Acoustics::FITKPhyscisPropertyUniform;
            _propMgr->appendDataObj(uniform);
        }
        if (!uniform)return;

        QString matName = _ui->comboBox_material->currentText();
        double thickness = _ui->doubleSpinBox_Thickness->value();

        if (_execType == GUI::PhysicsDlgExecType::Edit)
        {
            str.append(QString("p=PhyscisProp.Uniform.GetUniform('%1')").arg(uniform->getDataObjectName()));
            str.append(QString("p.setName('%1')").arg(name));
        }
        str.append(QString("p.setMaterial('%1')").arg(matName));
        str.append(QString("p.setThickness(%1)").arg(thickness));

        //保存数据  
        uniform->setDataObjectName(name);
        uniform->setThickness(thickness);
        uniform->setMaterialID(_ui->comboBox_material->currentData().toInt());

        this->saveScript(str);
        this->accept();
    }
    void PhysicsDialogUniform::on_pushButtonCancel_clicked()
    {
        this->reject();
    }

    void PhysicsDialogUniform::init()
    {
        if (_propMgr == nullptr || _matMgr == nullptr) return;

        if (_execType == GUI::PhysicsDlgExecType::Create)
        {
            QString name = _propMgr->checkName("New Physics Property Uniform");
            _ui->lineEdit_name->setText(name);
        }
        if (_prop == nullptr) return;
        Acoustics::FITKPhyscisPropertyUniform* uniform = dynamic_cast<Acoustics::FITKPhyscisPropertyUniform*>(_prop);
        if (!uniform) return;
        // 设置各个属性的值
        _ui->lineEdit_name->setText(uniform->getDataObjectName());
        int id1 = uniform->getMaterialID();
        int index1 = _ui->comboBox_material->findData(id1);
        _ui->comboBox_material->setCurrentIndex(index1);
        _ui->doubleSpinBox_Thickness->setValue(uniform->getThickness());

        //复制,更新名称
        if (_execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            QString text = QString("Copy of %1").arg(uniform->getDataObjectName());
            _ui->lineEdit_name->setText(text);
        }
    }

    bool PhysicsDialogUniform::checkInput()
    {
        if (_propMgr == nullptr) return false;
        //检查名称
        QString text = _ui->lineEdit_name->text().trimmed();
        if (text != "") {
            QString newText = _propMgr->checkName(text);
            if (text == newText) return true;
            //与自己重名
            if (_propMgr->getDataByName(text) == _prop) return true;
        }
        QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a new Name."));
        return false;
    }
}

